
function y = difBFS_fixedW(w,t,N,P,X,Z,R,C)
y = 0;
PZZ = calculatePHat_fixedW(w,t,X,Z,R,C);
for i = 1: t
    PP(:,:) = P(i,:,:);
    NN(:,:) = N(i,:,:);
    PZZZ(:,:) = PZZ(i,:,:);
        
    for ii=1:R
        for jj=1:C
            y = y + myfunc(ii,jj,NN,PP,PZZZ);
        end
    end
end

end



function v = myfunc(ii,jj,N,P,PZ)

p = P(ii,jj);
pHat = PZ(ii,jj);
n = N(ii,jj);

if pHat ~=0
    v = n * (p - pHat)^2 / pHat*(1-pHat) ;
else
    v = 0;
end

end

